package com.ssyt.hiring.company.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.Validate;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.common.util.AppUtil;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.company.entity.CompanyEntity;
import com.ssyt.hiring.company.service.ApplicationService;
import com.ssyt.hiring.company.service.CompanyService;

/**
 * CompanyServlet
 * 
 * @author Administrator
 *
 */
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CompanyService companyService = new CompanyService();
	private ApplicationService applicationService = new ApplicationService();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("toLoginUI".equals(command)) {
			toLoginUI(request, response);
		} else if ("toRegisterUI".equals(command)) {
			toRegisterUI(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("updatePwd".equals(command)) {
			updatePwd(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		}
	}

	/**
	 * 修改企业信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Object> list = new ArrayList<>();
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String linkman = request.getParameter("linkman");
		String address = request.getParameter("address");
		int numbers = Integer.parseInt(request.getParameter("numbers"));
		String slogan = request.getParameter("slogan");
		list.add(name);
		list.add(email);
		list.add(phone);
		list.add(linkman);
		list.add(address);
		list.add(numbers);
		list.add(slogan);
		list.add(id);
		int row = companyService.update(list);
		CompanyEntity company = companyService.preUpdate(id);
		request.getSession().setAttribute("company", company);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 修改查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Validate.notBlank(request.getParameter("id"), "Company修改前查询的id不能为空");
		int id = Integer.parseInt(request.getParameter("id"));
		CompanyEntity company = companyService.preUpdate(id);
		if (company != null) {
			request.setAttribute("com", company);
			request.getRequestDispatcher("/WEB-INF/view/company/update.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updatePwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String oldPassword = request.getParameter("password1");
		String newPassword = request.getParameter("password2");

		int row = companyService.updatePwd(oldPassword, newPassword, id);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 到主页时的查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pageNoStr = request.getParameter("pageNo");
		int pageNo = 1;
		if (pageNoStr != null && !"".equals(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		String rowStr = request.getParameter("row");
		// 非登录操作
		if (rowStr != null) {
			int row = Integer.parseInt(rowStr);
			if (row == 1) {
				request.setAttribute("success", "操作成功！");
			} else {
				request.setAttribute("error", "操作失败！");
			}
		}
		int id = AppUtil.getCompanyId(request, response);
		// 求职列表
		PageModel<ApplicationEntity> pageModel = applicationService.applyList(
				id, pageNo, Constants.PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("/WEB-INF/view/company/main.jsp").forward(
				request, response);
	}

	/**
	 * 跳转到登录界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toRegisterUI(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		request.getRequestDispatcher(
				"/WEB-INF/view/company/company-register.jsp").forward(request,
				response);
	}

	/**
	 * 跳转到登录界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void toLoginUI(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		request.getRequestDispatcher("/WEB-INF/view/company/company-login.jsp")
				.forward(request, response);
	}

	/**
	 * 企业者登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String validateCode = request.getParameter("validateCode");
		String vcode = (String) request.getSession().getAttribute("vcode");
		if (vcode.equals(validateCode)) {
			String code = request.getParameter("code");
			String password = request.getParameter("password");
			CompanyEntity company = companyService.login(code, password);
			if (company != null) {
				request.getSession().setAttribute("company", company);
				response.sendRedirect("CompanyServlet?command=main");
			} else {
				request.setAttribute("msg", "帐号或密码错误，请重新输入！");
				request.setAttribute("code", code);
				toLoginUI(request, response);
			}
		} else {
			request.setAttribute("msg", "验证码输入错误！");
			toLoginUI(request, response);
		}
	}

	/**
	 * 企业注册
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String validateCode = request.getParameter("validateCode");
		String vcode = (String) request.getSession().getAttribute("vcode");
		if (vcode.equals(validateCode)) {
			String code = request.getParameter("code");
			CompanyEntity company = companyService.queryByCode(code);
			if (company != null) {
				request.setAttribute("msg", "账号已被注册，请输入新的账号！");
				request.getRequestDispatcher(
						"/WEB-INF/view/company/company-register.jsp").forward(
						request, response);
			}
			List<Object> list = new ArrayList<>();
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String linkman = request.getParameter("linkman");
			String address = request.getParameter("address");
			int numbers = Integer.parseInt(request.getParameter("numbers"));
			String slogan = request.getParameter("slogan");
			list.add(code);
			list.add(password);
			list.add(name);
			list.add(email);
			list.add(phone);
			list.add(linkman);
			list.add(address);
			list.add(numbers);
			list.add(slogan);
			int row = companyService.add(list);
			if (row > 0) {
				request.setAttribute("msg", "注册成功，请登录！");
				response.sendRedirect("CompanyServlet?command=toLoginUI");
			} else {
				request.setAttribute("msg", "注册失败！");
				toRegisterUI(request, response);
			}
		} else {
			request.setAttribute("msg", "验证码输入错误！");
			toRegisterUI(request, response);
		}
	}

}
